﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Linq;
using DevExpress.LookAndFeel;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraEditors.Controls;
using XCI.Helper;
using XCI.Windows;
using XCI.Windows.Forms;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmTheme : FrmBase
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmTheme()
        {
            InitializeComponent();
        }

        public string FontName { get; set; }
        public string SkinStyle { get; set; }
        public string SkinName { get; set; }

        private void InitData()
        {
            if (DesignMode) return;
            SkinHelper.InitSkinGallery(galleryControl1, true, true);

            radioLookAndFeelStyle.Properties.Items.BeginUpdate();
            radioLookAndFeelStyle.Properties.Items.Add(new RadioGroupItem("Native", "Native"));
            var sz = Enum.GetNames(typeof(LookAndFeelStyle));
            foreach (string s in sz)
            {
                radioLookAndFeelStyle.Properties.Items.Add(new RadioGroupItem(s, s));
            }

            radioLookAndFeelStyle.Properties.Items.EndUpdate();
            radioLookAndFeelStyle.BackColor = this.BackColor;

            radioLookAndFeelStyle.EditValue = UserLookAndFeel.Default.Style.ToString();
            editFont.Text = ObjectHelper.GetObjectString(DevExpress.Utils.AppearanceObject.DefaultFont);
            var skinName = UserLookAndFeel.Default.ActiveSkinName;
            var item = galleryControl1.Gallery.GetAllItems().FirstOrDefault(p => p.Tag.ToString().Equals(skinName));
            if (item != null)
            {
                item.Checked = true;
            }
        }

        private void LookAndFeelForm_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void linkLabSelectFont_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            fontDialog1.Font = DevExpress.Utils.AppearanceObject.DefaultFont;
            if (fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DevExpress.Utils.AppearanceObject.DefaultFont = fontDialog1.Font;
                FontName = editFont.Text = ObjectHelper.GetObjectString(fontDialog1.Font);
            }
        }

        private void radioLookAndFeelStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioLookAndFeelStyle.EditValue == null) return;

            string style = radioLookAndFeelStyle.EditValue.ToString();
            string skinName = UserLookAndFeel.Default.ActiveSkinName;
            var selectItems = galleryControl1.Gallery.GetCheckedItems();
            if (selectItems.Count > 0)
            {
                skinName = selectItems[0].Tag.ToString();
            }

            SkinStyle = style;
            switch (style)
            {
                case "Flat":
                    UserLookAndFeel.Default.SetFlatStyle();
                    break;
                case "Office2003":
                    UserLookAndFeel.Default.SetOffice2003Style();
                    break;
                case "Style3D":
                    UserLookAndFeel.Default.SetStyle3D();
                    break;
                case "UltraFlat":
                    UserLookAndFeel.Default.SetUltraFlatStyle();
                    break;
                case "Native":
                    UserLookAndFeel.Default.SetWindowsXPStyle();
                    break;
                case "Skin":
                    UserLookAndFeel.Default.SetSkinStyle(skinName);
                    break;
            }
        }

        private void galleryControlGallery1_ItemClick(object sender,
            DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {
            radioLookAndFeelStyle.SelectedIndex = 5;
            SkinName = e.Item.Tag.ToString();
            SkinStyle = radioLookAndFeelStyle.Text;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //保存
            //if (!string.IsNullOrEmpty(FontName)) SysRuntime.UserSetting.GlobalFontName = FontName;
            //if (!string.IsNullOrEmpty(SkinStyle)) SysRuntime.UserSetting.GlobalSkinStyle = SkinStyle;
            //if (!string.IsNullOrEmpty(SkinName)) SysRuntime.UserSetting.GlobalSkinName = SkinName;
            //Config.ConfigFactory.Default.Save();
            //SysRuntime.App.SaveUserSetting();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}